<template>
  <div class="dialog">
    <el-form :inline="true" :model="params" class="demo-form-inline">
      <el-form-item label="名称">
        <el-input placeholder="请输入名称" v-model="params.name"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch" style="margin-left: 100px">查询</el-button>
        <el-button type="primary" @click="onAdd" style="margin-left: 10px">添加用户组</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="tableData" border stripe style="width: 100%" v-loading="loading" size="small">
      <el-table-column prop="id" label="序号" align="center" width="90"> </el-table-column>
      <el-table-column prop="groupName" label="用户组名称" align="center"></el-table-column>
      <el-table-column prop="groupPermission" label="用户组权限" align="center"></el-table-column>
      <el-table-column prop="flag" label="状态" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.flag == 1" style="color: #67C23A;">启用</span>
          <span v-if="scope.row.flag == 0" style="color: #F56C6C;">禁用</span>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" align="center" width="160"></el-table-column>
      <el-table-column label="操作" align="center" fixed="right" width="200">
        <template slot-scope="scope">
          <el-button type="text" size="mini" @click="onEdit(scope.row)">编辑</el-button>
          <el-button type="text" size="mini" @click="onModifyStatus(scope.row)">删除</el-button>

        </template>
      </el-table-column>
    </el-table>
    <div class="block" style="float: left; margin-top: 20px">
      <div>
        <span style="
            padding-top: 10px;
            display: inline-block;
            vertical-align: middle;
            color: #606266;
            font-size: 14px;
          ">显示第{{ this.params.page }}页的{{ this.params.rows }}条记录，总共{{
            this.total
          }}条记录
        </span>
      </div>
    </div>
    <div class="block" style="float: right; margin-top: 20px">
      <el-pagination :current-page.sync="params.page" :page-sizes="[10, 20, 50, 100]" :page-size="params.rows" background
        layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange"
        @current-change="handleCurrentChange" />
    </div>

    <el-dialog :visible.sync="editShopVisible" title="添加用户组" width="800px" center>
      <el-form :model="data" ref="from" label-width="150px" :rules="rules">
        <el-form-item label="用户组名称:" prop="groupName">
          <el-input v-model="data.groupName"></el-input>
        </el-form-item>
        <!-- <el-form-item label="用户组权限:">
          <el-select v-model="data.groupPermission" multiple style="width:100%">
            <el-option label="无" :value="0"></el-option>
            <el-option v-for="item in permissionsList" :key="item.code" :label="item.name" :value="item.code"></el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item label="用户组权限:">
          <el-tree ref="tree" @check-change="checkChange" :data="permissionsList" :default-expand-all="true" :check-strictly="true"
            show-checkbox node-key="code"  :props="defaultProps">
            <!-- :default-checked-keys="['1000', '1001']" -->
          </el-tree>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editShopVisible = false">关 闭</el-button>
        <el-button type="primary" @click="handleEdit">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog :visible.sync="modifyVisible" title="提示" width="450px" center>
      <div class="del-dialog-cnt">确定删除用户组？</div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="modifyVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleModifyStatus">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>
<script>
import {
  getGroupList, editGroup, delGroup, getGroupDetail, getPermissionsList
} from "@/api/user";
import _ from 'lodash';

export default {
  data() {
    return {
      loading: false,
      currentPage4: 4,
      total: 0,
      params: {
        name: ''
      },
      tableData: [],
      permissionsList:[],
      editShopVisible: false,
      modifyVisible: false,
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      data: {
        id: 0,
        groupName: '',
        groupPermission: '',
        flag: 1,
      },
      rules: {
        groupName: [
          { required: true, message: '请输入名称', trigger: 'blur' },
        ]
      }
    };
  },

  methods: {
    onSearch() {
      this.getData();
    },
    handleSizeChange(val) {
      this.params.rows = val;
      this.getData();
    },
    handleCurrentChange(val) {
      this.params.page = val;
      this.getData();
    },
    getData(exportExcel) {
      if (exportExcel) {
        this.params.export = true
        // parkIncomeexport(this.params, "车场收入明细")
      } else {
        this.loading = true;
        this.params.export = false
        let params = {
          pageSize: this.params.rows,
          pageNo: this.params.page,
          filter: [{
            field: 'groupName',
            operator: 'LIKE',
            value: this.params.name
          }]
        }
        getGroupList(params).then((res) => {
          this.loading = false;
          this.total = res.data.total;
          this.tableData = res.data.rows;
        });
      }
    },
    onEdit(row) {
      this.resetForm();
      this.editShopVisible = true;
      var d = _.clone(row);
      d.groupPermission = d.groupPermission.split(',');      
      this.data = d;
      var that = this;
      setTimeout(function(){
        that.$refs.tree.setCheckedKeys(d.groupPermission)
      },1000)
    },
    handleEdit() {
      this.$refs['from'].validate((valid) => {
        if (valid) {
          editGroup(this.data).then((res) => {
            this.getData();
            this.editShopVisible = false;
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    onModifyStatus(row, flag) {
      this.modifyVisible = true;
      this.data.id = row.id;
    },
    handleModifyStatus() {
      delGroup(this.data.id).then((res) => {
        this.getData();
        this.modifyVisible = false;
      });
    },
    onAdd() {
      this.resetForm();
      this.editShopVisible = true;
      var that = this;
      setTimeout(function(){
        that.$refs.tree.setCheckedKeys([])
      },1000)
    },
    resetForm() {
      this.data = {
        id: 0,
        groupName: '',
        groupPermission: '',
        flag: 1,
      }
    },
    checkChange(a, b, c) {
      //如果为取消
      if (b === false) {
        //如果当前节点有子集
        if (a.children) {
          //循环子集将他们的选中取消
          a.children.map(item => {
            this.$refs.tree.setChecked(item.code, false);
          })
        }
      } else {
        //否则(为选中状态)
        //判断父节点id是否为空
        if (a.code !== '') {
          //如果不为空则将其选中
          this.$refs.tree.setChecked(a.code, true);
        }
      }
      // let checkedKeys = this.$refs.tree.getCheckedKeys();
      this.data.groupPermission = this.$refs.tree.getCheckedKeys();
    }
  },
  mounted() {
    this.getData();
    getPermissionsList().then((res) => {
      this.permissionsList = res.data
    });
  },
};
</script>
<style scoped lang="scss">
.my-radio {
  margin-bottom: 10px;
}

.dialog {
  width: 95%;
  height: 100%;
  padding-top: 30px;
  margin-left: 60px;
}
</style>
